<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Mporting extends CI_Model {

  public function jc($ds)
  {
    // $this->db->select('channel_id, yyyymm, d2 as d1, d3 as d2, d4 as d3, d5 as d4, d6 as d5, d7 as d6, d1 as d7');
    $this->db->select('channel_id, yyyymm, d1, d2, d3, d4, d5, d6, d7');
    $this->db->order_by('jc_id', 'asc');
    $query = $this->db->get($ds);
    $ds = substr($ds, 5, 15);
    foreach ($query->result() as $res) {
      $res->sales_id = $ds;
    }
    return $query->result();
  }

  public function get_all_ds()
  {
    $this->db->select('user_id');
    $this->db->where('user_group_id', 9);
    $this->db->where('istatus', 1);
    $this->db->where('f_get_cluster(user_id)', 60);
    $query = $this->db->get('t_mtr_user');
    $ds = $query->result('array');
    $query->free_result();

    // select * from information_schema.tables where TABLE_NAME = 't_jc_80002113'
    foreach ($ds as $key) {
      $this->db->select('TABLE_NAME', false);
      $this->db->where('TABLE_NAME', '\'t_jc_'.$key["user_id"].'\'', false);
      $query = $this->db->get('information_schema.tables', true);
      if($query->num_rows() == 1)
      {
        $data[] = $query->row();
      }
    }
    return $data;
  }

  public function do_port($sales_id, $channel_id, $year, $month, $d, $value)
  {
    $this->db->trans_begin();

    $info = array(
        'user_id' => $sales_id,
        'channel_id' => $channel_id,
        'yyyymm' => $year.$month
      );

    $exist = $this->_is_row_exist($info);

    $this->db->select("f_get_territory_id_by_channel_id('$channel_id') as territory_id");
    $territory_id = $this->db->get()->row()->territory_id;

    if($territory_id){
      if($exist == true)
      { 
        $data = array(
            'user_id' => $sales_id,
            'territory_id' => $territory_id,
            'channel_id' => $channel_id,
            'yyyymm' => $year.$month,
            'd'.$d => $value,
            'updated_by' => 'yaddi-admin',
            'updated_on' => date('Y-m-d H:i:s')
          );
        // echo 'update yyyymm: '.$year.$month.' sales_id: '.$sales_id.' territory_id: '.$territory_id.' channel_id: '.$channel_id.' d'.$d.' = '.$value.'<br>';
        echo 'update: '.json_encode($data).'<br>';
        $this->_update_row($data, $exist); 
        // die();
      }
      else
      { 
        $data = array(
            'user_id' => $sales_id,
            'territory_id' => $territory_id,
            'channel_id' => $channel_id,
            'yyyymm' => $year.$month,
            'd'.$d => $value,
            'created_by' => 'yaddi-admin',
            'created_on' => date('Y-m-d H:i:s')
          );
        // echo 'insert yyyymm: '.$year.$month.' sales_id: '.$sales_id.' territory_id: '.$territory_id.' channel_id: '.$channel_id.' d'.$d.' = '.$value.'<br>';
        echo '<strong>insert: '.json_encode($data).'</strong><br>';
        $this->_insert_row($data); 
        // die();
      }
    }

    if ($this->db->trans_status() === FALSE)
    {
        $this->db->trans_rollback();
    }
    else
    {
        $this->db->trans_commit();
    }
  }


  private function _is_row_exist($info)
  {
    $this->db->select('jc_id');
    $data = $this->db->get_where('t_jc_9', $info );
    if ($data->num_rows() > 0 )
      { return $data->row()->jc_id; }
    else
      { return false; }
  }

  private function _update_row($data, $jc_id)
  {
    $this->db->where('jc_id', $jc_id);
    $this->db->update('t_jc_9', $data);
  }

  private function _insert_row($data)
  {
    $this->db->insert('t_jc_9', $data);
  }


}

/* End of file porting.php */
/* Location: ./application/models/porting.php */
